namespace ASPNET.StarterKit.IssueTracker.BusinessLogicLayer
{
    using System;
    using System.Collections;
    using System.Reflection;

    public class ITUserCollection : CollectionBase
    {
        public int Add(ITUser value)
        {
            return this.List.Add(value);
        }

        public bool Contains(ITUser value)
        {
            return this.List.Contains(value);
        }

        public int IndexOf(ITUser value)
        {
            return this.List.IndexOf(value);
        }

        public void Insert(int index, ITUser value)
        {
            this.List.Insert(index, value);
        }

        protected override void OnInsert(int index, object value)
        {
            if (value.GetType() != Type.GetType("ASPNET.StarterKit.IssueTracker.BusinessLogicLayer.ITUser"))
            {
                throw new ArgumentException("value must be of type ITUser.", "value");
            }
        }

        protected override void OnRemove(int index, object value)
        {
            if (value.GetType() != Type.GetType("ASPNET.StarterKit.IssueTracker.BusinessLogicLayer.ITUser"))
            {
                throw new ArgumentException("value must be of type ITUser.", "value");
            }
        }

        protected override void OnSet(int index, object oldValue, object newValue)
        {
            if (newValue.GetType() != Type.GetType("ASPNET.StarterKit.IssueTracker.BusinessLogicLayer.ITUser"))
            {
                throw new ArgumentException("newValue must be of type ITUser.", "newValue");
            }
        }

        protected override void OnValidate(object value)
        {
            if (value.GetType() != Type.GetType("ASPNET.StarterKit.IssueTracker.BusinessLogicLayer.ITUser"))
            {
                throw new ArgumentException("value must be of type ITUser.");
            }
        }

        public void Remove(ITUser value)
        {
            this.List.Remove(value);
        }

        public void Sort(UserFields sortField, bool isAscending)
        {
            switch (sortField)
            {
                case UserFields.Username:
                    this.InnerList.Sort(new UsernameComparer());
                    break;

                case UserFields.DisplayName:
                    this.InnerList.Sort(new DisplayNameComparer());
                    break;

                case UserFields.RoleName:
                    this.InnerList.Sort(new RoleNameComparer());
                    break;
            }
            if (!isAscending)
            {
                this.InnerList.Reverse();
            }
        }

        public ITUser this[int index]
        {
            get
            {
                return (ITUser) this.List[index];
            }
            set
            {
                this.List[index] = value;
            }
        }

        private sealed class DisplayNameComparer : IComparer
        {
            public int Compare(object x, object y)
            {
                ITUser first = (ITUser) x;
                ITUser second = (ITUser) y;
                return first.DisplayName.CompareTo(second.DisplayName);
            }
        }

        private sealed class RoleNameComparer : IComparer
        {
            public int Compare(object x, object y)
            {
                ITUser first = (ITUser) x;
                ITUser second = (ITUser) y;
                return first.RoleName.CompareTo(second.RoleName);
            }
        }

        public enum UserFields
        {
            InitValue,
            Username,
            DisplayName,
            RoleName
        }

        private sealed class UsernameComparer : IComparer
        {
            public int Compare(object x, object y)
            {
                ITUser first = (ITUser) x;
                ITUser second = (ITUser) y;
                return first.Username.CompareTo(second.Username);
            }
        }
    }
}

